﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Services.Interfaces;
using Silverlight.Weblog.Common.IoC;

namespace Silverlight.Weblog.Client.DAL.Services
{
    public class AnalyzeNavigationFragment : IAnalyzeNavigationFragment
    {
        public AnalyzeNavigationFragment()
        {
            IoC.BuildUp(this);
        }

        public Dictionary<string, string> GetFragmentQueryString(string fragment)
        {
            Dictionary<string, string> ReturnValues = new Dictionary<string, string>();

            if (string.IsNullOrEmpty(fragment) || string.IsNullOrEmpty(fragment.TrimStart('#')))
                return new Dictionary<string, string>();

            fragment = fragment.TrimStart('#');

            string[] keyValuePairs = fragment.Split('&');

            foreach (var keyValuePair in keyValuePairs)
            {
                string[] keyValue = keyValuePair.Split('=');
                ReturnValues.Add(keyValue[0], keyValue[1]);
            }

            return ReturnValues;
        }

        public bool IsHomePage(Dictionary<string, string> fragmentQueryString)
        {
            return fragmentQueryString.ContainsKey("Home") || !fragmentQueryString.Any();
        }

        public bool IsSearchPage(Dictionary<string, string> fragmentQueryString)
        {
            return fragmentQueryString.ContainsKey("Search");
        }

        public bool IsCategoryPage(Dictionary<string, string> fragmentQueryString)
        {
            return fragmentQueryString.ContainsKey("Category");
        }

        public bool IsBlogPostPage(Dictionary<string, string> fragmentQueryString)
        {
            return fragmentQueryString.ContainsKey("BlogPost");
        }
    }
}